package com.example.x.dnuifood_929dx.controller;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.x.dnuifood_929dx.R;
import com.example.x.dnuifood_929dx.adapter.ShopAdapter;
import com.example.x.dnuifood_929dx.bean.ShopBean;
import com.example.x.dnuifood_929dx.fragment.SearchFragment;
import com.example.x.dnuifood_929dx.fragment.CollectFragment;
import com.example.x.dnuifood_929dx.fragment.HomeFragment;
import com.example.x.dnuifood_929dx.fragment.MeFragment;

import java.security.acl.Group;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by X on 2018/11/15.
 */

public class MainActivity extends AppCompatActivity {
    private ImageButton ib1, ib2, ib3, ib4;
    private TextView tv1, tv2, tv3, tv4;
    private Group gp1, gp2, gp3, gp4;
    private ViewPager vp1;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
    private RecyclerView recyclerView;
    private List<ShopBean> shopBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ib1 = (ImageButton) findViewById(R.id.ib1);
        ib2 = (ImageButton) findViewById(R.id.ib2);
        ib3 = (ImageButton) findViewById(R.id.ib3);
        ib4 = (ImageButton) findViewById(R.id.ib4);
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);
        tv4 = findViewById(R.id.tv4);
/* gp1 = findViewById(R.id.group1);
gp2 = findViewById(R.id.group2);
gp3 = findViewById(R.id.group3);*/
        vp1 = (ViewPager) findViewById(R.id.vp1);
        initEvents();
        initFragment();
        ShopAdapter mAdapter = new ShopAdapter(this, listfragment);
// recyclerView = findViewById(R.id.rv01);
//recyclerView.setLayoutManager(neLinearLayoutManager(this));//?
// recyclerView.setItemAnimator(new DefaultItemAnimator());
// shopBeans = getData();
// recyclerView.setAdapter(new ShopAdapter(this,shopBeans));
    }
    private void resetImage() {
        ib1.setImageResource(R.drawable.sy);
        ib2.setImageResource(R.drawable.sc);
        ib3.setImageResource(R.drawable.ss);
        ib4.setImageResource(R.drawable.wd);
    }
    private void selectTab(int i) {//viewparge左滑右滑
        switch (i) {
            case 0:
                ib1.setImageResource(R.drawable.sy);
                break;
            case 1:
                ib2.setImageResource(R.drawable.sc);
                break;
            case 2:
                ib3.setImageResource(R.drawable.ss);
                break;
            case 3:
                ib4.setImageResource(R.drawable.wd);
                break;
        }
        vp1.setCurrentItem(i);//设置其初始显示的⻚页⾯面
    }
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {//点击跳转
//先将四个按钮都设成未选图⽚片
            resetImage();
            switch (view.getId()) {
                case R.id.ib1:
                case R.id.tv1:
                    selectTab(0);
                    break;
                case R.id.ib2:
                case R.id.tv2:
                    selectTab(1);
                    break;
                case R.id.tv3:
                case R.id.ib3:
                    selectTab(2);
                    break;
                case R.id.tv4:
                case R.id.ib4:
                    selectTab(3);
                    break;
            }
        }
    };
    private void initEvents() {
        ib1.setOnClickListener(onClickListener);
        ib2.setOnClickListener(onClickListener);
        ib3.setOnClickListener(onClickListener);
        ib4.setOnClickListener(onClickListener);
        tv1.setOnClickListener(onClickListener);
        tv2.setOnClickListener(onClickListener);
        tv3.setOnClickListener(onClickListener);
        tv4.setOnClickListener(onClickListener);
    }
    private void initFragment() {
        listfragment = new ArrayList<Fragment>();
        listfragment.add(new HomeFragment());
        listfragment.add(new CollectFragment());
        listfragment.add(new SearchFragment());
        listfragment.add(new MeFragment());
        fpadapter = new
                FragmentPagerAdapter(getSupportFragmentManager()) {
                    @Override
                    public Fragment getItem(int position) {
                        return listfragment.get(position);
                    }
                    @Override
                    public int getCount() {
                        return listfragment.size();
                    }
                };
        vp1.setAdapter(fpadapter);
        vp1.addOnPageChangeListener(new
                                            ViewPager.OnPageChangeListener() {//数据填充
                                                @Override
                                                public void onPageScrolled(int position, float positionOffset, int
                                                        positionOffsetPixels) {
                                                }
                                                @Override
                                                public void onPageSelected(int position) {
                                                    vp1.setCurrentItem(position);
                                                    resetImage();
                                                    selectTab(position);
                                                }
                                                @Override
                                                public void onPageScrollStateChanged(int state) {
                                                }
                                            });
    }
    private List<ShopBean> getData() {
        List<ShopBean> list = new ArrayList();
        ShopBean shopBean = new ShopBean();
        shopBean.setShopname("满⼝口川湘菜");
        shopBean.setAddress("东软三期⻝⾷食堂");
        shopBean.setPic(R.drawable.ic_launcher_background + "");
        shopBean.setLevel(5);
        list.add(shopBean);
        shopBean = new ShopBean();
        shopBean.setShopname("东北北⼀一家⼈人");
        shopBean.setAddress("东软三期⻝⾷食堂");
        shopBean.setPic(R.drawable.ic_launcher_background + "");
        shopBean.setLevel(2);
        list.add(shopBean);
        shopBean = new ShopBean();
        shopBean.setShopname("姐妹麻辣烫");
        shopBean.setAddress("东软三期⻝⾷食堂");
        shopBean.setPic(R.drawable.ic_launcher_background + "");
        shopBean.setLevel(3);
        list.add(shopBean);
        shopBean = new ShopBean();
        shopBean.setShopname("⻄西湖印象");
        shopBean.setAddress("东软三期⻝⾷食堂");
        shopBean.setPic(R.drawable.ic_launcher_background + "");
        shopBean.setLevel(4);
        list.add(shopBean);
        shopBean = new ShopBean();
        shopBean.setShopname("王⼦子普");
        shopBean.setAddress("东软三期⻝⾷食堂");
        shopBean.setPic(R.drawable.ic_launcher_background + "");
        shopBean.setLevel(2);
        list.add(shopBean);
        return list;
    }
}